package org.manekineko.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class StringUtils {

	public static void print(Integer[] arg0) {
		String s = "";
		for (int i = 0; i < arg0.length; i++) {
			s += ", " + arg0[i];
		}
		System.out.println(s.substring(2));
	}

	public static String hash(String arg0) {
		return hash(splitAsIntArray(arg0), 6);
	}

	public static String hash(String arg0, int size) {
		return hash(splitAsIntArray(arg0), size);
	}

	public static String hash(List<Integer> arg0, int size) {
		Collections.sort(arg0);
		Integer[] result = new Integer[arg0.size()];
		arg0.toArray(result);
		return hash(result, size);
	}

	public static String hash(Integer[] arg0, int size) {
		if (size > arg0.length) {
			size = arg0.length;
		}

		StringBuilder result = new StringBuilder();
		for (int i = 0; i < size; i++) {
			result.append("#" + arg0[i]);
		}

		return DatabaseUtils.hash(result.toString().substring(1));
	}

	public static Integer[] splitAsIntArray(String arg0) {
		return splitAsIntArray(arg0, false);
	}

	public static Integer[] splitAsIntArray(String arg0, boolean isOrdered) {
		if (arg0 == null) {
			return null;
		}

		String[] str = arg0.split("[,;]");

		return splitAsIntArray(arg0, str.length, isOrdered);
	}

	public static Integer[] splitAsIntArray(String arg0, int size, boolean isOrdered) {
		if (arg0 == null) {
			return null;
		}

		String[] str = arg0.split("[,;]");

		if (size > str.length) {
			size = str.length;
		}

		Integer[] result = new Integer[size];
		for (int i = 0; i < size; i++) {
			result[i] = Integer.valueOf(str[i]);
		}

		if (isOrdered) {
			Arrays.sort(result);
		}

		return result;
	}

	public static List<Integer> splitAsList(String arg0) {
		List<Integer> result = new ArrayList<Integer>();
		
		if (arg0 == null) {
			return null;
		}

		Integer[] dezenas = splitAsIntArray(arg0);
		for (Integer dezena : dezenas) {
			result.add(dezena);
		}

		return result;
	}

	
	public static void main(String[] args) {
		Integer[] a = new Integer[] { 6, 16, 20, 27, 44, 53 };
		Integer[] b = new Integer[] { 8, 9, 11, 48, 53, 60 };

		System.out.println(hash(a, 6));
		System.out.println(hash(b, 6));

		System.exit(0);

		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(new File("sena.csv")));
			String line = null;
			while ((line = br.readLine()) != null) {
				Integer[] p1 = splitAsIntArray(line);
				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < p1.length; i++) {
					sb.append(", " + p1[i]);
				}
				System.out.println(hash(p1, 6) + " " + sb.toString().substring(2));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
